package com.zzg.srb.getway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import java.util.Collections;

/**
 * @Author zhengzg
 * @Date 2022/6/18 18:59
 * @Version 1.0
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //可接受的域，是一个具体域名或者*（代表任意域名）
        config.setAllowedOriginPatterns(Collections.singletonList("*"));
        //允许携带的头
        config.addAllowedHeader("*");
        //允许访问的方式
        config.addAllowedMethod("*");
        //是否允许携带cookie
        config.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
